package parasoft.centrap.test;

import java.util.Collection;
import java.util.HashMap;
import java.util.Set;

public class Helper {
	private final HashMap map = new HashMap();

	public static final class Builder{
		private HashMap map = new HashMap();

		private Builder(){
			
		}
		
		public static Builder create(){
			return new Builder();
		}
		public Builder reset(){
			map.clear();
			return this;
		}
		
		public Builder set(String key, Object v){
			if(v instanceof String){
				return set(key, (String)v);
			}else{
				p(key, v);
			}
			return this;
		}

		public Builder set(String key, String v){
			p(key, v);return this;
		}

		public Builder set(String key, boolean v){
			p(key, Boolean.valueOf(v));return this;
		}

		public Builder set(String key, int v){
			p(key, new Integer(v));return this;
		}

		public Builder set(String key, long v){
			p(key, new Long(v));return this;
		}

		public Builder set(String key, float v){
			p(key, new Float(v));return this;
		}

		public Builder set(String key, double v){
			p(key, new Double(v));return this;
		}

		private void p(String k, Object v){
			if(k!=null){
				map.put(k, v);
			}
		}

		public Helper build(){
			return new Helper(this);
		}
	}

	private Helper(Builder b){
			map.putAll(b.map);
	}

	public boolean containsKey(String key){
		return (key!=null && map!=null && map.containsKey(key));
	}
	
	public boolean containsValue(String value){
		return (value!=null && map!=null && map.containsValue(value));
	}
	
	public Set keySet(){
		return (map!=null)?map.keySet():null;
	}
	
	public Set entrySet(){
		return (map!=null)?map.entrySet():null;
	}
	
	public Collection values(){
		return map.values();
	}
	public boolean is(String key){
		return is(key, false);
	}

	public boolean is(String key,boolean def){
		Object o = get(key);
		if(o instanceof Boolean){
			return ((Boolean)o).booleanValue();
		}
		if(o instanceof String){
			final String s = (String)o;
			if(s.trim().length()>0){
				return (s.toLowerCase().equals("true")||s.toLowerCase().equals("tak")||
						s.toLowerCase().equals("yes")||s.toLowerCase().equals("t"));
			}
		}
		return def;
	}

	public String getString(String k){
		return getS(k);
	}

	private String getS(String k){
		final Object o = get(k);
		if(o!=null){
			return o.toString();
		}
		return null;
	}

	public int getInt(String k){
		return getInt(k, 0);
	}

	public int getInt(String k, int def){
		final Object o = get(k);
		if(o instanceof Integer){
			return ((Integer)o).intValue();
		}
		if(o!=null){
			final String s = o.toString();
			if(s.trim().length()>0){
				try {
					return Integer.parseInt(s);
				} catch (NumberFormatException e) {
					return def;
				}
			}
		}
		return def;
	}

	public long getLong(String k){
		return getLong(k, 0);
	}

	public long getLong(String k, long def){
		final Object o = get(k);
		if(o instanceof Long){
			return ((Long)o).longValue();
		}
		if(o!=null){
			final String s = o.toString();
			if(s.trim().length()>0){
				try {
					return Long.parseLong(s);
				} catch (NumberFormatException e) {
					return def;
				}
			}
		}
		return def;
	}

	public float getFloat(String k){
		return getFloat(k, 0);
	}

	public float getFloat(String k, float def){
		final Object o = get(k);
		if(o instanceof Float){
			return ((Float)o).floatValue();
		}
		if(o!=null){
			final String s = o.toString();
			if(s.trim().length()>0){
				try {
					return Float.parseFloat(s);
				} catch (NumberFormatException e) {
					return def;
				}
			}
		}
		return def;
	}

	public double getDouble(String k){
		return getDouble(k, 0);
	}

	public double getDouble(String k, double def){
		final Object o = get(k);
		if(o instanceof Double){
			return ((Double)o).doubleValue();
		}
		if(o!=null){
			final String s = o.toString();
			if(s.trim().length()>0){
				try {
					return Double.parseDouble(s);
				} catch (NumberFormatException e) {
					return def;
				}
			}
		}
		return def;
	}
	public Object get(String k){
		if(k!=null && map.containsKey(k)){
			return map.get(k);
		}
		return null;
	}

}
